<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="org.bson.Document" %>
<%@ page import="com.mongodb.client.*" %>
<%@ page import="java.util.regex.Pattern" %>
<%@ page import="org.bson.conversions.Bson" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.LinkedList" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>添加学生</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <!-- 时间选择器样式表 -->
    <link href="https://cdn.bootcss.com/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css"
          rel="stylesheet">
</head>
<body>

<jsp:include page="major.jsp"></jsp:include>

<%
    String url = "mongodb://g08:abc%408@mongo.zhledu.cn:27017/g08";
    MongoClient client = MongoClients.create(url);
    MongoIterable<String> names = client.listDatabaseNames();
    MongoDatabase g08 = client.getDatabase("g08");
    MongoCollection<Document> collection = g08.getCollection("student");
    //查询系统中有多少个专业
    List<String> majorList = (List<String>) session.getAttribute("majorList");

    //查出要修改的学生信息
    String sno = request.getParameter("sno");
    Document query = new Document("sno", sno);
    final FindIterable<Document> list = collection.find(query).limit(1);
    Document student = new Document();
    for (Document s : list) {
        student = s;
        break;
    }


%>
<div class="container">
    <center>
        <h1>添加学生</h1>
    </center>
    <form class="form-horizontal" method="post" action="upload/update.jsp">
        <div class="form-group">
            <label class="control-label col-sm-2">学号:</label>
            <div class="col-sm-4">
                <input type="hidden" id="_id" value="<%=student.getInteger("_id")%>" name="_id">
                <input id="sno" name="sno" class="form-control" value="<%=sno%>" readonly>
            </div>
            <label class="control-label col-sm-2"> 姓名:</label>
            <div class="col-sm-4">
                <input id="name" name="name" class="form-control" value="<%=student.getString("name")%>">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-sm-2"> 身份证号:</label>
            <div class="col-sm-4">
                <input id="idCard" name="idCard" class="form-control" value="<%=student.getString("idCard")%>">
            </div>
            <label class="control-label col-sm-2"> 电话:</label>
            <div class="col-sm-4">
                <input id="phone" name="phone" type="tel" class="form-control" value="<%=student.getString("phone")%>">
            </div>
        </div>
        <div class="form-group">

            <label class="control-label col-sm-2"> 年龄:</label>
            <div class="col-sm-4">
                <input id="age" name="age" class="form-control" value="<%=student.getInteger("age")%>">
            </div>
            <label class="control-label col-sm-2"> 出生日期:</label>
            <div class="col-sm-4">
                <input id="birthDay" type="date" name="birthDay" class="form-control"
                       value="<%=student.getString("birthDay")%>">
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-sm-2"> 性别:</label>
            <div class="col-sm-4">
                <select id="sex" name="sex" class="form-control">
                    <option value="">全部</option>
                    <option value="男" <%=student.getString("sex").equals("男") ? "selected" : ""%>>男</option>
                    <option value="女" <%=student.getString("sex").equals("女") ? "selected" : ""%>>女</option>
                </select>
            </div>


            <label class="control-label col-sm-2"> 用户名:</label>
            <div class="col-sm-4">
                <input id="username" name="username" class="form-control" readonly required
                       value="<%=student.getString("username")%>">
            </div>
        </div>


        <div class="form-group">
            <label class="control-label col-sm-2"> 密码:</label>
            <div class="col-sm-4">
                <input id="password" name="password" type="password" class="form-control" required>
            </div>
            <label class="control-label col-sm-2"> 确认密码:</label>
            <div class="col-sm-4">
                <input id="password2" name="password2" type="password" class="form-control" required>
            </div>

        </div>


        <div class="form-group">
            <label class="control-label col-sm-2"> 电子邮件:</label>
            <div class="col-sm-4">
                <input id="email" name="email" type="email" class="form-control"
                       value="<%=student.getString("email")%>">
            </div>

            <label class="control-label col-sm-2"> 专业:</label>
            <div class="col-sm-4">
                <select id="major" name="major" class="form-control">
                    <option value="">全部</option>
                    <%
                        for (String m : majorList) {
                    %>
                    <option value="<%=m%>" <%=student.getString("major").equals(m) ? "selected" : ""%>><%=m%>
                    </option>
                    <%
                        }
                    %>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-sm-2"> 家庭地址:</label>
            <div class="col-sm-10">
                <textarea class="form-control" id="address" name="address"
                          value="<%=student.getString("address")%>"></textarea>
            </div>
        </div>
        <center>

            <button id="submit" type="submit" class="btn btn-info glyphicon glyphicon-save">保存</button>
            <button type="reset" class="btn btn-warning glyphicon glyphicon-refresh">重置</button>
            <button type="button" class="btn btn-primary glyphicon glyphicon-home" id="index">回首页</button>
        </center>
    </form>
</div>


<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
<!-- 时间选择器前置脚本 -->
<script src="https://cdn.bootcss.com/moment.js/2.22.1/moment-with-locales.min.js"></script>
<!-- 时间选择器核心脚本 -->
<script src="https://cdn.bootcss.com/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

<script>
    $(function () {
        $("#submit").submit(function () {
            if ($("#password").val() != $("#password2").val()) {
                /*swal("您输入的两次密码不一致");*/
                alert("您输入的两次密码不一致")
                return false;
            }


        });

        $("#birthDay").datetimepicker({
            format: 'YYYY-MM-DD',
            locale: moment.locale('zh-CN')
        });

        $("#idCard").on("change", function () {
            var idCard = $(this).val();
            let patten = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
            if (patten.test(idCard)) {
                var bitrhday = idCard.substring(6, 10) + "-" + idCard.substring(10, 12) + "-" + idCard.substring(12, 14);
                $("#birthDay").val(bitrhday);
                $("#birthDay").attr("readonly", true);
                var age = new Date().getFullYear() - parseInt(idCard.substring(6, 10));
                $("#age").val(age);
                $("#age").attr("readonly", true);

                var sex = parseInt(idCard.substring(16, 17)) % 2 == 0 ? "女" : "男";
                $("#sex").val(sex);
                $("#sex").attr("readonly", true);
            } else {
                $("#birthDay").attr("readonly", false);
                $("#age").attr("readonly", false);
                $("#sex").attr("readonly", false);
            }
        });


    })
</script>

<%
    client.close();
%>
</body>
</html>
